package com.example.aaa;


import android.app.AlarmManager;
import android.app.DatePickerDialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.icu.util.Calendar;
import android.icu.util.TimeZone;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;


public class  AddActivity extends AppCompatActivity implements View.OnClickListener {
    private Button back;  //返回按钮
    private CheckBox notif;  //返回按钮
    private EditText title;   //标题
    private EditText content;   //内容
    private ImageView BGTime;//开始时间
    private ImageView FNTime;//结束时间
    private TextView BG_Time_content;
    private TextView FN_Time_content;
    private Spinner sp_dropdown;
    private String strBGTime;
    private String strBGDate;
    private String strBG;
    private String strFNTime;
    private String strFNDate;
    private String strFN;
    private int statu;
    private int Hour;
    private int Minute;

    private String[] learingSiteArrar = {"学习", "工作", "日常", "生活"};

    private int TongzhiId = 1;

    private Button finish;  //完成按钮
    private String get_title;
    private String get_content;
    private String get_createTime;
    private String get_finishTime;
    private String get_statu;
    private AdapterView.OnItemSelectedListener onItemSelectedListener = new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int postion, long I) {
            switch (adapterView.getId()) {
                case R.id.sp_dropdown:
                    //记住点击的选项
                    statu = postion;
                    break;
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);

        back = (Button) findViewById(R.id.back_add);
        notif = (CheckBox) findViewById(R.id.notif);
        title = (EditText) findViewById(R.id.title_add);
        content = (EditText) findViewById(R.id.context_add);
        finish = (Button) findViewById(R.id.finish);
        BGTime = (ImageView) findViewById(R.id.BG_Time_Right_jv);
        FNTime = (ImageView) findViewById(R.id.FN_Time_Right_jv);
        BG_Time_content = (TextView) findViewById(R.id.BG_Time_content);
        FN_Time_content = (TextView) findViewById(R.id.FN_Time_content);
        sp_dropdown = (Spinner) findViewById(R.id.sp_dropdown);

        finish.setOnClickListener(this);
        back.setOnClickListener(this);
        BGTime.setOnClickListener(this);
        FNTime.setOnClickListener(this);


        intiDropdownSpinner();
    }

    private void intiDropdownSpinner() {
        //初始化AdapterView
        Spinner sp_dropdown = (Spinner) findViewById(R.id.sp_dropdown);
        sp_dropdown.setPrompt("请选择待办类型");
        ArrayAdapter<String> learningSiteAdapter = new ArrayAdapter<String>(this,
                R.layout.item_select, learingSiteArrar);
        sp_dropdown.setAdapter(learningSiteAdapter);
        sp_dropdown.setSelection(0);
        sp_dropdown.setOnItemSelectedListener(onItemSelectedListener);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.finish: {
                NoteOperator noteOperator = new NoteOperator(AddActivity.this);
                get_title = title.getText().toString().trim();
                get_content = content.getText().toString().trim();
                get_createTime = BG_Time_content.getText().toString();
                get_finishTime = FN_Time_content.getText().toString();
                get_statu = statu + "";
                //判断输入是否有空项
                if (TextUtils.isEmpty(get_title) || TextUtils.isEmpty(get_content) || TextUtils.isEmpty(strBG)
                        || TextUtils.isEmpty(strFN) || TextUtils.isEmpty(get_statu)) {
                    Toast.makeText(AddActivity.this, "不能有空项", Toast.LENGTH_SHORT).show();
                } else {
                    Note note = new Note();
                    note.title = get_title;
                    note.context = get_content;
                    note.createTime = get_createTime;
                    note.finishTime = get_finishTime;
                    note.statu = statu + "";

                    boolean add = noteOperator.insert(note);
                    //如果添加数据成功，跳到待办事项界面，并通过传值，让目标界面进行刷新
                    if (add) {
                        //Toast.makeText(AddActivity.this,"添加成功",Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent();
                        intent.setClass(AddActivity.this, MainActivity.class);
                        intent.putExtra("Insert", 1);
                        Toast.makeText(AddActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                        startActivity(intent);
                        //将设置的标题和内容存入文件，待会通知会去取值
                        SharedPreferences sp = getSharedPreferences("notif", MODE_PRIVATE);
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putString("Title", get_title);
                        editor.putString("Content", get_content);
                        editor.commit();

                        finish();
                    } else {
                        Toast.makeText(AddActivity.this, "添加失败", Toast.LENGTH_SHORT).show();
                    }
                }

                //通过判断是否选择来发送通知
                if (notif.isChecked()) {
                    Intent intent = new Intent(AddActivity.this, AlarmReceiver.class);
                    AlarmReceiver.title = get_title;
                    AlarmReceiver.content = get_content;
                    startRemind();
                }


            }
            break;

            case R.id.BG_Time_Right_jv: {

                strBGDate = null;
                strBGTime = null;
                strBG = "";
                Calendar cal = Calendar.getInstance();
                //得到小时
                int hourOfDay = cal.get(Calendar.HOUR_OF_DAY);
                //得到分钟
                int minute = cal.get(Calendar.MINUTE);
                new TimePickerDialog(AddActivity.this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        Hour = hourOfDay;
                        Minute = minute;
                        strBGTime = " " + hourOfDay + ":" + minute;
                        if (strBGDate != null && strBGTime != null) {
                            strBG = strBGDate + strBGTime;

                        } else {
                            strBG = "";
                        }
                        //将选择的时间设置到TextView
                        BG_Time_content.setText(strBG);
                    }
                }, hourOfDay, minute, true).show();

                //得到年月日
                int year = cal.get(Calendar.YEAR);
                int month = cal.get(Calendar.MONTH);
                int day = cal.get(Calendar.DAY_OF_MONTH);
                new DatePickerDialog(AddActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                        strBGDate = year + "/" + (month + 1) + "/" + day + "";

                    }
                }, year, month, day).show();


            }
            break;

            case R.id.FN_Time_Right_jv: {

                strFNDate = null;
                strFNTime = null;
                strFN = null;

                Calendar cal = Calendar.getInstance();
                int hourOfDay = cal.get(Calendar.HOUR_OF_DAY);    //得到小时
                int minute = cal.get(Calendar.MINUTE);            //得到分钟
                new TimePickerDialog(AddActivity.this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        strFNTime = " " + hourOfDay + ":" + minute;
                        if (strFNDate != null && strFNTime != null) {
                            strFN = strFNDate + strFNTime;

                        } else {
                            strFN = "";
                        }
                        FN_Time_content.setText(strFN);
                    }
                }, hourOfDay, minute, true).show();

                //得到年月日
                int year = cal.get(Calendar.YEAR);
                int month = cal.get(Calendar.MONTH);
                int day = cal.get(Calendar.DAY_OF_MONTH);
                new DatePickerDialog(AddActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                        strFNDate = year + "/" + (month + 1) + "/" + day + "";

                    }
                }, year, month, day).show();


            }
            break;

            case R.id.back_add: {
                //返回按钮，返回主页面
                Intent intent = new Intent(AddActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
            break;

            default:
                break;


        }

    }

    //启动定时通知方法
    public void startRemind() {
        Calendar mCalendar = Calendar.getInstance();
        mCalendar.setTimeInMillis(System.currentTimeMillis());
        //获取当前毫秒值
        long systemTime = System.currentTimeMillis();
        //是设置日历的时间，主要是让日历的年月日和当前同步
        mCalendar.setTimeInMillis(System.currentTimeMillis());
        // 这里时区需要设置一下，不然可能个别手机会有8个小时的时间差
        mCalendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        //设置在几点提醒 设置的为选定的Hour
        mCalendar.set(Calendar.HOUR_OF_DAY, Hour);
        //设置在几分提醒 设置的为选定的Minute分
        mCalendar.set(Calendar.MINUTE, Minute);
        mCalendar.set(Calendar.SECOND, 0);
        mCalendar.set(Calendar.MILLISECOND, 0);
        //上面设置的就是小时和分钟的时间点
        //获取上面设置的时间
        long selectTime = mCalendar.getTimeInMillis();

        // 如果当前时间大于设置的时间，那么就从第二天的设定时间开始
        if (systemTime > selectTime) {
            mCalendar.add(Calendar.DAY_OF_MONTH, 1);
        }
        //AlarmReceiver.class为广播接受者
        Intent intent = new Intent(AddActivity.this, AlarmReceiver.class);
        PendingIntent pi = PendingIntent.getBroadcast(AddActivity.this, 0, intent, 0);
        //得到AlarmManager实例
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        /**
         * 单次提醒
         * mCalendar.getTimeInMillis() 上面设置的时间点毫秒值
         */
        am.set(AlarmManager.RTC_WAKEUP, mCalendar.getTimeInMillis(), pi);

    }
}




